package com.gage.controller.app;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gage.controller.base.BaseController;
import com.gage.commons.result.PageInfo;
import com.gage.commons.utils.StringUtils;
import com.gage.commons.utils.DateUtils;
import com.gage.model.BsAssets;
import com.gage.model.SysUser;
import com.gage.service.IBsAssetsService;
import com.gage.service.IUserService;

/**
 * <p>
 * 资产管理控制器
 * </p>
 * @author kairu.lu
 * @since 2017-04-26
 */
@Controller
@RequestMapping("/app/assets")
public class AppAssetsController extends BaseController{
	@Autowired
	private IBsAssetsService assetsService;
	@Autowired
	private IUserService userService;
	@GetMapping("/index")
    public String create() {
        return "app/assets/index";
    }
	/**
	 * 资产事项列表
     * @author lkr
     * @date:20170512
	 * @param assets
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @return
	 */
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(BsAssets assets, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        String associationId=this.getShiroUser().getAssociationId().toString();
        if (StringUtils.isNotEmpty(associationId)) {
            condition.put("associationId", associationId);
        }else {
        	 return pageInfo;
		}
        if (StringUtils.isNotEmpty(assets.getGoodsName())) {
            condition.put("goodsName", assets.getGoodsName());
        }
        pageInfo.setCondition(condition);
        assetsService.selectDataGrid(pageInfo);
        return pageInfo;
    }
    /**
     * 添加资产
     * @param departmentId
     * @param model
     * @return
     */
    @GetMapping("/add")
    public String addPage(Model model) {
    	SysUser user=userService.selectById(Integer.parseInt(this.getUserId()+""));
		model.addAttribute("userName",user.getName());
        return "app/assets/edit";
    }
    /**
     * 编辑查看资产页
     *
     * @return
     */
    @GetMapping("/edit")
    public String editPage(String id,Model model) {
    	if (StringUtils.isNotBlank(id)) {
			BsAssets assets=assetsService.selectById(Integer.parseInt(id));
			SysUser user=userService.selectById(assets.getUserId());
			model.addAttribute("userName",user.getName());
			model.addAttribute("assets",assets);
		}
        return "app/assets/edit";
    }
    /**
     * @author lkr
     * @date:20170512
     * @param leaguer
     * @param result
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
	public Object save(@Valid BsAssets assets, BindingResult result){
    	if (result.hasErrors()) {
            return renderError(result);
        }
    	assets.setAssociationId(this.getShiroUser().getAssociationId());
    	
    	if (StringUtils.isEmpty(assets.getAssociationId())) {
    		return resultJson("0","网络故障","");
		}
    	if (StringUtils.isNotEmpty(assets.getId())) {
    		assetsService.updateById(assets);
		}else {
			assets.setUserId(Integer.parseInt(this.getUserId()+""));
			assets.setInsertTime(new Date());
			assetsService.insert(assets);
		}
    	return resultJson("1","信息保存成功","");
    }
    /**
     * 删除
     * @author lkr
 	 * @date:20170512
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
    	if (StringUtils.isNotEmpty(id)) {
    		assetsService.deleteById(Integer.parseInt(id));
    		return renderError("操作成功！");
		}else {
			return renderError("删除失败，网络出错！");
		}
	}
}
